package me.luger.core.mvc.entity;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.commom.util.JsonUtil;
import me.luger.core.mvc.WebService;
import me.luger.core.mvc.log.access.AccessLogEntity;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/27 0027.
 */
public class Result {
    private int code;
    private String msg;
    private String reqId;
    private Object data;

    public Result(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        AccessLogEntity accessLogEntity = WebService.getAccessLogEntity();
        if(null != accessLogEntity){
            this.reqId = accessLogEntity.getLogId();
        }else{
            this.reqId = "unKnown";
        }

    }

    public static Result setIntCodeResult(IntCodeResult intCodeResult){
        return new Result(intCodeResult.getCode(),intCodeResult.getMsg(),null);
    }

    public static Result success(){
        return success(null);
    }

    public static Result success(Object data){
        return new Result(200,"success",data);
    }
    public static Result success(String msg, Object data){
        return new Result(200,msg,data);
    }

    public static Result error(){
        return error(null);
    }
    public static Result error(Object data){
        return new Result(-1,"error",data);
    }
    public static Result error(String msg, Object data){
        return new Result(-1,msg,data);
    }

    public static Result info(){
        return info(null);
    }
    public static Result info(Object data){
        return new Result(200,"",data);
    }
    public static Result info(String msg, Object data){
        return new Result(200,msg,data);
    }
    public static Result info(int code,String msg, Object data){
        return new Result(code,msg,data);
    }
    public static Result info(int code,String msg){
        return new Result(code,msg,null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getReqId() {
        return reqId;
    }

    public void setReqId(String reqId) {
        this.reqId = reqId;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Result addDateItem(String key,Object value){
        Object data = this.getData();
        Map<String,Object> dataMap = null;
        if(null == data){
            dataMap = new HashMap<String,Object>();
        }else if(data instanceof Map){
            dataMap = (Map<String,Object>)data;
        }

        dataMap.put(key,value);
        this.setData(dataMap);
        return this;
    }

    public static void main(String[] args){

        Long start = System.currentTimeMillis();

        for(int i=0;i<10000;i++){
            Map<String,Object> resultMap = new HashMap<String,Object>();
            resultMap.put("1","yi");
            resultMap.put("2","er");
            resultMap.put("3","san");
            Result result = Result.success(resultMap);
            result = null;
        }
        Long end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        for(int i=0;i<10000;i++){
            Result result = Result.success().addDateItem("1","yi").addDateItem("2","er");
            result.addDateItem("3","san");
            result = null;
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);
    }
}
